package com.antler.smt.portal.controller;

import com.antler.smt.entity.MemberEntity;
import com.antler.smt.portal.utils.TokenUtil;
import org.springframework.web.bind.annotation.ModelAttribute;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *  TODO
 *
 *  @author vic
 *  @date 2018-03-28 20:38
 *  
 */
public class BaseController {

    protected MemberEntity currentMember;


    /**
     * * 说明：ModelAttribute的作用,1)放置在方法的形参上：表示引用Model中的数据,放置在方法上面：表示请求该类的每个Action前都会首先执行它，也可以将一些准备数据的操作放置在该方法里面。
     * @param request
     * @param response
     */
    @ModelAttribute
    public void setReqAndRes(HttpServletRequest request, HttpServletResponse response) {

        String token = null;
        Cookie[] cookies = request.getCookies();
        if (cookies != null && cookies.length > 0) {
            for (Cookie cookie : cookies) {
                if (cookie.getName().equals("token")) {
                    token = cookie.getValue();
                    break;
                }
            }
        }
        if (token != null) {
            currentMember = TokenUtil.getMemberByToken(token);
        }
    }
}
